
$(function () {
    $("#up").uploadPreview({ Img: "ImgPr", Width: 750, Height: 390,size:300 });
});
$(".editbtn").each(function (index, item) {
    $(item).click(function () {
        var id =$($(".text").get(index)).val();
        var src=$($("img").get(index)).attr("src");
        var str=`您正在修改好货优选第${index+1}张轮播图`;
        var cateName=$($(".cateNameval").get(index)).val();
        console.log(cateName)
        $(".cateName").html(cateName)
        $("#myModalLabel").html(str);
        $("#first").html("");
        $("#section").html("");
        $("#three").html("");
        $.ajax({
            url:"getCategroy",
            data:{"type":"", "categroy":""},
            type:"post",
            dataType:"json",
            success:function (val) {
                var str=`<option value="" id="sectionclass">请选择一级分类</option>`;
                val=JSON.parse(val);
                for(var i=0;i<val.length;i++){
                    var cur=val[i];
                    str+=`<option name="${cur.id}"  value="${cur.categroy_name}">${cur.categroy_name}</option>`;
                }
                $("#first").html("");
                $("#first").html(str);
            }
        });
        $("#ImgPr").attr("src",src);
        $("#id").val(id);
    })
});
/*一级分类改变开始三级联动请求*/
$("#first").change(getsection);
$("#section").change(getthree);
function getsection() {
    var selectoption= $("#first option:selected");
    var firstname=selectoption.text();
    var id=selectoption.attr("name");
    var str=`<input type="text" hidden name="firstName" value="${firstname}"/>`
    $("#firstName").html(str);
    $.ajax({
        url:"getCategroy",
        data:{"type":1, "categroy":id},
        type:"post",
        dataType:"json",
        async:false,
        success:function (val) {
            var str=`<option value="" id="sectionclass">请选择二级分类</option>`;
            val=JSON.parse(val);
            for(var i=0;i<val.length;i++){
                var cur=val[i];
                str+=`<option name="${cur.id}"  value="${cur.categroy_name}">${cur.categroy_name}</option>`;
            }
            $("#section").html("");
            $("#section").html(str);

        }
    })
}

function getthree() {
    var selectoption= $("#section option:selected");
    var secondName=selectoption.text();
    var str=` <input type="text" hidden name="secondName" value="${secondName}"/>`
    $("#secondName").html(str)
    var id=selectoption.attr("name");
    $.ajax({
        url:"getCategroy",
        data:{"type":2, "categroy":id},
        type:"post",
        dataType:"json",
        async:false,
        success:function (val) {
            var str=`<option value="" id="threeclass">请选择三级分类</option>`;
            val=JSON.parse(val);
            for(var i=0;i<val.length;i++){
                var cur=val[i];
                str+=`<option name="${cur.id}"  value="${cur.categroy_name}">${cur.categroy_name}</option>`;
            }
            $("#three").html("");
            $("#three").html(str);
        }
    })
}
$("#three").change(function () {
    var $threeoption=$("#three option:selected");
    var id=$threeoption.attr("name");
    $("#threeid").val(id);
    var thirdName=$threeoption.text();
    var str=`<input type="text" hidden name="thirdName" value="${thirdName}"/>`
    $("#thirdName").html(str);
});

$("#bok").click(function () {
    if(!$("#up").val()){
        console.log($("input[name=thirdName]").val(),$("input[name=secondName]").val())
        alert("请选择图片");
        return false
    }else if (!$("#first option:selected").val()||!$("#section option:selected").val()||!$("#three option:selected").val()){
        alert("请选择分类");
        return false
    }
});